<template>
  <div class="login">
    <div class="login_box">
      <img class="login_box_img" src="@/assets/images/login/login_left.svg" />
    </div>
    <div class="login_form">
      <div class="login_form_title">
        <img src="@/assets/images/global/logo.png" alt="logo" />
        <span>云科源主项目</span>
      </div>
      <div class="login_form_content">
        <div class="login_form_content_title">登录</div>
        <el-form ref="formRef" label-position="top" :rules="rules" :model="formData" @keyup.enter="handleSubmit">
          <el-form-item label="组织" prop="tenantId">
            <el-select v-model="formData.tenantId" clearable placeholder="请选择">
              <el-option v-for="item in tenantData" :key="item.code" :label="item.name" :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="用户名" prop="userCode">
            <el-input v-model="formData.userCode" autocomplete="off" clearable placeholder="请输入" prefix-icon="User"
              type="text">
            </el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="formData.password" :show-password="true" autocomplete="off" placeholder="请输入"
              prefix-icon="Lock" type="password">
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button :loading="loading" style="margin: auto" type="primary" @click="handleSubmit">
              <span>登录</span>
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup>
import { apiGetTenantList, apiLogin } from '@/apis/login'
import { useUserStore } from '@/store/user'
import { useSystemStore } from '@/store/system.js'
import { computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { addTrendsRouteList } from '@/router/utils.js'
const router = useRouter()

const formData = reactive({
  tenantId: undefined,
  userCode: 'admin',
  password: '1',
})

const rules = computed(() => {
  return {
    tenantId: [
      {
        required: formData.userCode !== 'root',
        message: '请选择组织',
        trigger: 'change',
      },
    ],
    userCode: [
      {
        required: true,
        message: '请输入用户名',
        trigger: 'blur',
      },
    ],
    password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
  }
})
const tenantData = ref({})

// 表单ref
const formRef = ref()
// 登录按钮
const loading = ref(false)
const handleSubmit = async () => {
  const { token } = storeToRefs(useUserStore())
  const { getUserInfo } = useUserStore()
  const { routerTagList } = storeToRefs(useSystemStore())
  await formRef.value.validate()
  loading.value = true
  try {
    const { accessToken } = await apiLogin(formData)
    token.value = accessToken
    routerTagList.value = []
    // 获取用户信息
    await getUserInfo()
    // 获取路由
    await addTrendsRouteList()
    ElMessage.success('登录成功')
  } finally {
    loading.value = false
  }
  router.push('/home')
}

const getTenantList = async () => {
  tenantData.value = await apiGetTenantList()
  if (tenantData.value.length > 0) {
    formData.tenantId = tenantData.value[0]['id']
  }
}
getTenantList()
</script>

<style lang="scss" scoped>
.login {
  display: flex;
  align-items: center;
  position: relative;
  height: 100vh;
  width: 100vw;
  background: #1f76cb;
  overflow: hidden;

  &_box {
    // height: 100vh;
    flex: 1;
    text-align: center;

    &_img {
      width: 70%;
    }
  }

  &_form {
    width: 40%;
    flex-shrink: 0;

    &_title {
      display: flex;
      align-items: center;
      margin-bottom: 30px;

      img {
        width: 52px;
        // height: 52px;
      }

      span {
        margin-left: 10px;
        font-size: 28px;
        font-weight: bold;
        color: #fff;
      }
    }

    &_content {
      width: 500px;
      padding: 20px 50px;
      box-sizing: border-box;
      border-radius: 4px;
      background-color: #fff;

      &_title {
        margin-bottom: 20px;
        font-size: 24px;
        font-weight: bold;
      }
    }
  }
}
</style>
